package com.qianli.cashmama.trade.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * 国际化工具类
 *
 * @author: moss
 * 2018/6/6
 */
@Component("msgManager")
public class MessageManager {

    private static MessageSource messageSource;


    @Autowired
    public void setMessageSource(MessageSource messageSource) {
        MessageManager.messageSource = messageSource;
    }


    public static String getMsg(String key) {
        //Locale locale = LocaleContextHolder.getLocale();
//        Locale locale = Locale.US;
//        return messageSource.getMessage(key, null, locale);
        return key;
    }

    /**
     * @param arg 替换key中的占位符 {0} {1} ....
     * @return
     */
    public static String getMsg(String key, String... arg) {
        Object[] args = new Object[arg.length];
        System.arraycopy(arg, 0, args, 0, arg.length);
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(key, args, locale);
    }


}
